<template>
  <div class="tenantMenu_main main_container add_container_table AddOrUpdate_main add_container">
    <div v-loading="loading" class="main_main">
      <div ref="table_container" class="table_container">
        <div style="margin-bottom:20px" class="SearchBar_main main_main_search">
          <div class="main_main_search_item">
            <!-- <a class="button" @click="add">
              新增
            </a> -->
          </div>
          <div class="main_main_search_item">
            <el-input v-model="pageData.searchValue" class="search_input" placeholder="请输入订单号或物料编码" clearable @keyup.enter.native="()=>{pageData.pageNum=1; search()}" @clear="getList" />
            <el-button class="button_icon" icon="el-icon-search" @click.stop="()=>{pageData.pageNum=1; search()}" />
          </div>
        </div>
        <el-table
          ref="table"
          :data="tableData"
          stripe
          border
          :height="400"
          :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
          @sort-change="(column)=>{sortChange(column, this)}"
          @selection-change="(val)=>{selectionChange(val, this)}"
        >
          <el-table-column type="selection" align="center" />
          <Column v-for="(column,index) in defaultTableColumns" :key="index" :prop="column.prop" :is-show="column.isShow" :label="column.label" :sortable="column.sortable" :width="column.width" :min-width="column.minWidth" />
        </el-table>
      </div>
    </div>
    <!-- <div class="addtype-main_bottoms">
      <pagination
        :total="total"
        :page.sync="pageData.pageNum"
        :limit.sync="pageData.pageSize"
        :selected-num="selectionsAll.length"
        @pagination="getList"
      />
    </div> -->
    <div class="main_bottom">
      <el-button type="cancel" @click.stop="cancel">取消</el-button>
      <el-button @click.stop="onSubmit">保存</el-button>
    </div>
  </div>
</template>

<script>
import { getSearchInfo } from '@/api/saleManage/saleOrder'
export default {
  props: {
    layerid: {},
    id: {},
    selectRow: {}
  },
  data() {
    return {
      loading: false,
      pageData: {
        pageNum: 1,
        pageSize: 20,
        orderByColumn: 'updateTime',
        isAsc: 'desc',
        searchValue: ''
      },
      tableDataOne: [],
      tableData2: [],
      // tableKey: 1,
      total: 1, // 数据一共有多少条
      mainId: 'id', // 表格行ID
      tableHeight: '300', // 表格高度
      tableData: [], // 表格数据
      tableColumns: [], // 表格实际用的字段
      searchColumns: [], // 搜索区实际用的字段
      selections: [], // 表格当前页所选中的行
      selectionsAll: [], // 表格所有页选中的行
      defaultTableColumns: [// 表格常规字段（初始值）
        { prop: 'saleCustomerOrderCode', label: '客户订单号', minWidth: '120' },
        { prop: 'saleCustomerDeliveryTime', label: '客户交期', minWidth: '140' },
        { prop: 'saleCustomerMaterialCode', label: '客户料号', minWidth: '100' },
        { prop: 'materialCode', label: '物料编码', minWidth: '120' },
        { prop: 'materialName', label: '物料名称', minWidth: '150' },
        { prop: 'materialModel', label: '规格型号', minWidth: '100' },
        { prop: 'materialUnitName', label: '物料单位', minWidth: '100' },
        { prop: 'materialInventory', label: '库存数量', minWidth: '100' },
        { prop: 'orderNumber', label: '订单数量', minWidth: '100' },
        // { prop: 'deliverNumber', label: '本次发货数量' },
        { prop: 'alreadyDeliverNumber', label: '已发数量', minWidth: '100' },
        { prop: 'remark2', label: '备注', minWidth: '200' }

      ]
    }
  },
  created() {
  },
  mounted() {
    this.getList()
    this.selectionsAll = this.selectRow
  },
  methods: {
    getList() {
      getSearchInfo(this.id).then(res => {
        res.data.data.forEach(ele => {
          ele.saleOrderDetailId = ele.id
          // delete ele.id
        })
        this.tableData2 = this.$deepClone(res.data.data)
        res.data.data.filter(item => {
          item.remark2 = item.remark
          item.remark = ''
          this.selectionsAll.filter(ele => {
            if (item.saleOrderDetailId == ele.saleOrderDetailId) {
              item.returnNumber = ele.returnNumber
              item.remark = ele.remark
            }
          })
        })
        this.tableData = res.data.data
        this.loading = false
        this.$nextTick(() => {
          this.setSelectRow(this)// 设置行选中
          this.tableHeight = this.$refs.table_container.offsetHeight
        })
      })
    },
    search() {
      const arr = JSON.parse(JSON.stringify(this.selectionsAll))
      this.tableDataOne = this.tableData2.filter(item => item.materialCode && item.materialCode.indexOf(this.pageData.searchValue) !== -1 || item.saleCustomerOrderCode && item.saleCustomerOrderCode.indexOf(this.pageData.searchValue) !== -1)
      this.tableData = this.tableDataOne
      this.$nextTick(() => {
        this.selectionsAll = arr
        this.setSelectRow(this)
      })
    },
    cancel() {
      this.$layer.close(this.layerid)
    },
    onSubmit() {
      // this.selectionsAll.forEach(ele => {
      //   delete ele.remark
      // })
      this.$parent.showListData(this.selectionsAll)
      this.$layer.close(this.layerid)
    }
  }
}
</script>

<style lang="less" scoped>
.button{
    width: 100px;
    height: 30px;
    color: #fff;
    background-color: #387bfc;
    border-radius: 4px;
    text-align: center;
    line-height: 30px;
}
.main_search_item{
  padding-right: 20px !important;
}
.main_main_search{
  padding-right: 20px !important;
}
.main_main_search_item{
  padding-left: 20px !important;
}
.main_main{
  margin:0 !important;
  padding-top:0 !important;
}
.button_icon{
 background:#387bfc;
 width:30px;
}
.search_input{
  width:200px;
  margin-right:10px
}
.table_container{
  margin: 0 !important;
}
.addtype-main_bottoms{
  padding-right: 20px !important;
  margin: 10px 0;
}
/deep/ .el-input--suffix .el-input__inner {
    padding-right:25px;
}
</style>
